<?php
/**
 * $Id$
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * @version 			$Rev$
 * @lastModifiedBy		$LastChangedBy$
 * @lastChanged			$Date$
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

class ConfigController extends ConfigAppController {

	var $name='Config';
	var $helpers = array('Html','Form','Javascript');
	//var $uses = array();


	/**
	 * Function to change backend settings. Don't touch without permission.
	 *
	 */
	function admin_settings()
	{
		if(!Configure::read('Config.editable'))
		{
			$this->warningMsg(__('No access!'));
			$this->redirect($this->referer());
		}
		$this->pageTitle = __("System Configuration");
		if($this->data)
		{
			$options = array();
			$options['initialData'] = false;
			if($this->saveConfig($options))
			{
				$this->successMsg(__('Configuration saved successfully!'));
				$this->redirect('/admin');
			}
			else
			{
				//$this->errorMsg(__('Configuration could not be saved!'));
			}
		}else
		{
			$configs = Configure::read('cmsConfig');
			$data = array();
			foreach($configs as $group => $config)
			{
				$data = am($data,$config);
			}
			$this->data['Config'] = $data;
		}
	}

	private function saveConfig($saveOptions = array())
	{
		$return = true;
		if($this->Config->saveConfig($this->data,$saveOptions))
		{
			
		}else
		{
			$return = false;
			$errorMsg = __("There are some errors in data.Please correct them for tabs.");
			if(isset($this->Config->validationErrors['configFile']))
			{
				$errorMsg = __("Cannot save config file.").$this->Config->validationErrors['configFile'];
			}
			$this->errorMsg($errorMsg);
		}
		return $return;
	}

	/**
	 * This function installs the CMS when it is run for the first time
	 *
	 */
	function firstRun()
	{
		include  CORE_PATH . 'cake' . DS . 'config' .DS.'vendor_info.php';
		$this->pageTitle = $config['Vendor']['product'].' '.$config['Vendor']['version'].' Installer';
		$this->layout = 'plain';
		callHooks('BeforeInstall', null, $this);

		// check if config file already exists
		if(file_exists(CONFIG_FILE))
		{
			echo __("CMS is already installed");
			exit;
		}

		// check if folder writable
		if(!is_writable(APP.'config'))
		{
			$this->warningMsg(__("Config folder is not writable."));
		}

		//prd($this);
		if($this->data)
		{
			$options = array("initialData"=>true);
			if(isset($this->data['Config']['initial_data']) && $this->data['Config']['initial_data'] == 'N-o')
			{
				$options['initialData'] = false;
			}
			if($this->saveConfig($options))
			{
				callHooks('AfterInstall', null, $this);
				if(defined('SHELL_INSTALL'))
				{
					return true;
				}
				$this->successMsg(__('Installation successful!'));
				$this->redirect("/");
			}else
			{
				
			}
		}
		$this->set('installation', true);
		$this->render('admin_settings');
	}
	
	function _validateConnection($data)
	{		
		$link = @mysql_connect($data['Database']['host'], $data['Database']['login'], $data['Database']['password']);
		if(!$link)
		{
			return false;
		}
		return true;
	}

	function error()
	{
		if($errors = $this->Session->read('error'))
		{
			if(is_string($errors))
				$errors = array($errors);
			$this->set('errors',$errors);
			$this->Session->del('error');
		}
		else {
			$this->redirect('/');
		}
	}
}

?>
